package com.zzz.gp.service.analyse;

import com.zzz.gp.bean.GpHistory;
import com.zzz.gp.dao.GpMapper;
import com.zzz.gp.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: ZZZ
 * @time: 2021/12/16 15:16
 */
@Slf4j
@Service
public class ADailyLimit1Service {
    @Autowired
    private GpMapper gpMapper;


    @Async("myAsync")
    public void init() {
        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String today=dtf.format(LocalDateTime.now());
        String path="/home/"+today+"/Z1.txt";
        LocalDateTime s = LocalDateTime.now();
        List<String> adds = new ArrayList<>();
        List<GpHistory> list = gpMapper.getTodayLimitUpDataAll(today);
        list.forEach(x -> {
            String code = x.getCode();
            analyse(code, adds);

        });
        if (adds.size() > 0) {
            StringBuilder sb = new StringBuilder("");
            adds.forEach(x -> {
                sb.append(",").append(x);
            });
            String p = path ;
            FileUtil.txtWriter(p, sb.substring(1));
        }
        LocalDateTime e = LocalDateTime.now();
        long seconds = Duration.between(s, e).getSeconds();
        log.info("a-daily-limit-1分析 用时{}秒,符合数量{}支", seconds, adds.size());
        log.info(adds.toString());
    }

    public void analyse(String code, List<String> adds) {
        //统计当天所有涨停
        adds.add(code);

        /*int limit = 2;
        List<GpHistory> list = gpMapper.getHistoryData(code, limit);
        //返回的条数满足 limit
        if (list.size() != limit) {
            return;
        }

        //double a = Double.parseDouble(list.get(0).getIncrease_p());
        double b = Double.parseDouble(list.get(1).getIncrease_p());

        if (b < 9) {
            adds.add(code);
        }*/
    }

}
